﻿using NetCore.BLL;
using NetCore.Code.Model;
using NetCore.Log;
using NetCore.Model;
using NetCore.ORM;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NetCore.Code.BLL
{
    public class InitDataEntity
    {
        public List<UserEntity> userEntities { get; set; }


        public List<DictionaryEntity> dicEntities { get; set; }

        public List<TemplateEntity> tempEntities { get; set; }

        public List<RoleEntity> roleEntities { get; set; }

        public List<PermissionEntity> perEntities { get; set; }

        public List<RelationUserEntity> relEntities { get; set; }

        public List<SerialRuleEntity> numEntities { get; set; }

        public List<SerialRuleNumberEntity> numerEntities { get; set; }

        public List<DbFieldRelationTypeEntity> dbTypeEntities { get; set; }
        public List<SysFieldTypeEntity> codeDbTypeEntities { get; set; }
        public List<TemplateEntity> templateEntities { get; set; }

        public List<RegionEntity> regionEntities { get; set; }

        public List<DataVaildEntity> vaildEntities { get; set; }

        public List<DbTemplateEntity> dbTemplateEntities { get; set; }

    }


    public class InitDataService : SerivceBase {

        public static string InitPath
        {
            get {
                return ApplicationEnvironments.BaseDirectory + "/config/initdata/initdata.json";
            }
        }
        public void Init()
        {
            InitDataEntity initData = new InitDataEntity();
            initData.userEntities = new UserService().GetAll();
            initData.roleEntities = new RoleService().GetAll();
            initData.perEntities = new PermissionService().GetAll();
            initData.tempEntities = new TemplateService().GetAll();
            initData.dicEntities = new DictionaryService().GetAll();
            initData.numerEntities = new SerialRuleNumberService().GetAll();
            initData.relEntities = new RelationUserService().GetAll();
            initData.numEntities = new SerialRuleService().GetAll();
            initData.dbTypeEntities = new DbFieldRelationTypeService().GetAll();
            initData.codeDbTypeEntities = new SysFieldTypeService().GetAll();
            initData.templateEntities = new TemplateService().GetAll();
            initData.regionEntities = new RegionService().GetAll();
            initData.vaildEntities = new DataVaildService().GetAll();
            initData.dbTemplateEntities = new DbTemplateService().GetAll();
            FileHelper.WriteLine(InitPath, initData.ToJson());

        }
        public bool ExistInit()
        {
            return File.Exists(InitPath);
        }
        public void InsertData(IDbRepository dbRepository)
           {
       
            InitDataEntity initData = null;
            FileInfo info = new FileInfo(InitPath);
            if (!info.Exists)
            {
              
                return;
            }
            string jsonData= FileHelper.ReadLine(InitPath);
            if (!string.IsNullOrWhiteSpace(jsonData))
            {
                initData = JSONHelper.FromJson<InitDataEntity>(jsonData);
            }
            if (initData != null)
            {
                using (var tran= dbRepository.Begin())
                {
                    try
                    {
                        if (initData.userEntities != null && initData.userEntities.Count > 0)
                        {
                            initData.userEntities.ForEach(x=> {
                                x.IsUpdated = false;
                            });
                             dbRepository.BatchSave<UserEntity>(initData.userEntities);
                        }
                       
                        if (initData.perEntities != null && initData.perEntities.Count > 0)
                        {
                            initData.perEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<PermissionEntity>(initData.perEntities);
                        }
                        if (initData.relEntities != null && initData.relEntities.Count > 0)
                        {
                            initData.relEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<RelationUserEntity>(initData.relEntities);
                        }
                        if (initData.roleEntities != null && initData.roleEntities.Count > 0)
                        {
                            initData.roleEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<RoleEntity>(initData.roleEntities);
                        }
                        if (initData.tempEntities != null && initData.tempEntities.Count > 0)
                        {
                            initData.tempEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<TemplateEntity>(initData.tempEntities);
                        }
                        if (initData.dicEntities != null && initData.dicEntities.Count > 0)
                        {
                            initData.dicEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<DictionaryEntity>(initData.dicEntities);
                        }
                        if (initData.numEntities != null && initData.numEntities.Count > 0)
                        {
                            initData.numEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<SerialRuleEntity>(initData.numEntities);
                        }
                        if (initData.numerEntities != null && initData.numerEntities.Count > 0)
                        {
                            initData.numerEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<SerialRuleNumberEntity>(initData.numerEntities);
                        }
                        if (initData.dbTypeEntities != null && initData.dbTypeEntities.Count > 0)
                        {
                            initData.dbTypeEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<DbFieldRelationTypeEntity>(initData.dbTypeEntities);
                        }
                        if (initData.regionEntities != null && initData.regionEntities.Count > 0)
                        {
                            initData.regionEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<RegionEntity>(initData.regionEntities);
                        }
                        if (initData.codeDbTypeEntities != null && initData.codeDbTypeEntities.Count > 0)
                        {
                            initData.codeDbTypeEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<SysFieldTypeEntity>(initData.codeDbTypeEntities);
                        }
                        if (initData.vaildEntities != null && initData.vaildEntities.Count > 0)
                        {
                            initData.vaildEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<DataVaildEntity>(initData.vaildEntities);
                        }
                        if (initData.dbTemplateEntities != null && initData.dbTemplateEntities.Count > 0)
                        {
                            initData.dbTemplateEntities.ForEach(x => {
                                x.IsUpdated = false;
                            });
                            dbRepository.BatchSave<DbTemplateEntity>(initData.dbTemplateEntities);
                        }
                        dbRepository.Commit();
                        LogHelper.Info("同步成功",this);
                    }
                    catch (Exception ex)
                    {
                        dbRepository.Rollback();
                        LogHelper.Info("同步失败", ex);
                        throw ex;
                    }
                }
            }
           // info.Delete();
        }

        public bool DeleteInitConfig()
        {
            FileInfo info = new FileInfo(InitPath);
            if (info.Exists)
            {
                info.Delete();
                return true;
            }
            return false;
        }
    }
}
